package com;

import java.util.ArrayList;
import java.util.List;


public class MovieService {
    private List<Movie> movieList;
    private LogService logService;

    public MovieService(List<Movie> movieList, LogService logService) {
        this.movieList = movieList;
        this.logService = logService;                                 }
    public boolean addMovie(Movie movie, User operator) {
        for (Movie m : movieList) {
            if (m.getMovieId().equals(movie.getMovieId())) {
                System.out.println("错误：影片ID已存在");
                return false;                              }
                                  }
        movieList.add(movie);
        logService.recordLog("添加影片", operator, "添加影片《" + movie.getTitle() + "》");
        return true;                                    }

    public boolean deleteMovie(String movieId, User operator, ScheduleService scheduleService) {
        List<Schedule> schedules = scheduleService.getSchedulesByMovieId(movieId);
        if (!schedules.isEmpty()) {
            System.out.println("错误：该影片存在关联场次，无法删除");
            return false;         }
        Movie movieToRemove = null;
        for (Movie m : movieList) {
            if (m.getMovieId().equals(movieId)) {
                movieToRemove = m;
                break;}           }
        if (movieToRemove != null) {
            movieList.remove(movieToRemove);
            logService.recordLog("删除影片", operator, "删除影片《" + movieToRemove.getTitle() + "》");
            return true;           }
        System.out.println("错误：影片不存在");
        return false;                                                                           }
    public boolean updateMovie(Movie updatedMovie, User operator) {
        for (int i = 0; i < movieList.size(); i++) {
            if (movieList.get(i).getMovieId().equals(updatedMovie.getMovieId())) {
                movieList.set(i, updatedMovie);
                logService.recordLog("更新影片", operator, "更新影片《" + updatedMovie.getTitle() + "》");
                return true;}                      }
        System.out.println("错误：影片不存在");
        return false;}
    public Movie getMovieById(String movieId) {
        for (Movie m : movieList) {
            if (m.getMovieId().equals(movieId)) {
                return m;                       }
                                  }
        return null;                          }
    public List<Movie> getAllMovies() {
        return movieList;             }
    public List<Movie> searchMovies(String keyword) {
        List<Movie> result = new ArrayList<>();
        keyword = keyword.toLowerCase();


        return result;}                                               }
